package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.ConferenceDTO;
import com.eastfair.imaster.dto.ConferencePageQuery;
import com.eastfair.imaster.dto.ConferenceSaveDTO;
import com.eastfair.imaster.dto.ConferenceUpdateDTO;
import com.eastfair.imaster.entity.Conference;
import com.eastfair.imaster.service.ConferenceService;
import com.eastfair.imaster.vo.ConferenceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 论坛表
 * </p>
 *
 * @author zjn
 * @date 2021-08-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/conference")
@Api(value = "Conference", tags = "论坛表")
public class ConferenceController extends SuperController<ConferenceService, Long, Conference, ConferencePageQuery, ConferenceSaveDTO, ConferenceUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private ConferenceService conferenceService;

    @Override
    public void handlerResult(IPage<Conference> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Conference> conferenceList = list.stream().map((map) -> {
            Conference conference = Conference.builder().build();
            //TODO 请在这里完成转换
            return conference;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(conferenceList));
    }

    /**
     * 新增论坛
     *
     * @param conferenceDTO 论坛
     * @return 主键
     */
    @ApiOperation(value = "新增论坛", notes = "新增论坛")
    @PostMapping("/saveConference")
    public R<Boolean> saveConference(@RequestBody @Validated({ConferenceDTO.VerifyInsert.class}) ConferenceDTO conferenceDTO) {
        return conferenceService.saveConference(conferenceDTO);
    }

    /**
     * 删除论坛
     *
     * @param id 论坛主键
     * @return 是否删除成功
     */
    @ApiOperation(value = "删除论坛", notes = "删除论坛")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteById(@PathVariable("id") Long id) {
        return conferenceService.deleteById(id);
    }

    /**
     * 修改论坛
     *
     * @param conferenceDTO 论坛
     * @return 是否修改成功
     */
    @ApiOperation(value = "修改论坛", notes = "修改论坛")
    @PutMapping("/updateById")
    public R<Boolean> updateById(@RequestBody @Validated({ConferenceDTO.VerifyUpdate.class}) ConferenceDTO conferenceDTO) {
        return conferenceService.updateById(conferenceDTO);
    }

    /**
     * 通过id查询论坛
     *
     * @param id 通过id查询论坛
     * @return 论坛实体
     */
    @ApiOperation(value = "通过id查询论坛", notes = "通过id查询论坛")
    @GetMapping("/queryById/{id}")
    public R<ConferenceVO> queryById(@PathVariable("id") Long id) {
        return conferenceService.queryById(id);
    }

    /**
     * 获取论坛列表
     *
     * @param conferenceDTO 论坛
     * @return 论坛列表
     */
    @ApiOperation(value = "获取论坛列表", notes = "获取论坛列表")
    @PostMapping("/queryList")
    public R<List<ConferenceVO>> queryList(@RequestBody ConferenceDTO conferenceDTO) {
        return R.success(conferenceService.queryList(conferenceDTO));
    }

    /**
     * 获取分页论坛列表
     *
     * @param params 分页条件
     * @return 分页论坛列表
     */
    @ApiOperation(value = "获取分页论坛列表", notes = "获取分页论坛列表")
    @PostMapping("/queryPageList")
    public R<Page<ConferenceVO>> queryPageList(@RequestBody PageParams<ConferencePageQuery> params) {
        return R.success(conferenceService.queryPageList(params));
    }

    /**
     * 启用禁用
     *
     * @param conferenceDTO 论坛
     * @return 启用禁用
     */
    @ApiOperation(value = "修改论坛", notes = "修改论坛")
    @PostMapping("/updateEnabledById")
    public R<Boolean> updateEnabledById(@RequestBody @Validated({ConferenceDTO.VerifyEnabled.class}) ConferenceDTO conferenceDTO) {
        return conferenceService.updateEnabledById(conferenceDTO);
    }

}
